﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;

namespace TinyCinema.Data
{
    public class TicketOrder:Execution
    {
        public string ID { get; set; }
        public decimal Value { get; set; }
        public Customer Customer { get; set; }
        public Employee Employee { get; set; }
        public ObservableCollection<Ticket> Tickets { get; set; }
        public DateTime OrderDate { get; set; }
        public TicketOrder(Customer customer,Employee employee,Schedule schedule, ObservableCollection<Desk> lstDesk)
        {
            ID = IDGenerate(Tables.TicketOrder);
            if (customer != null)
                Customer = customer;
            else Customer = new Customer("KH000000");
            if (employee != null)
                Employee = employee;

            Tickets = new ObservableCollection<Ticket>();
            Value = schedule.TypePrice.Price;
            Data.Promotion CurrentPromotion = null;
            var lstPromotion = Data.Promotion.GetAllPromotion();  
            foreach (var promotion in lstPromotion)
            {
                if (promotion.StartDate <= DateTime.Now && promotion.EndDate >= DateTime.Now)
                {
                    CurrentPromotion = promotion;
                    break;
                }
            }
            foreach (var Desk in lstDesk)
            {
                Value += Desk.Type.AdditionalPrice;               
                Value = decimal.Parse((double.Parse(Value.ToString()) * Customer.Type.Discount).ToString());                
                decimal TicketPrice = schedule.TypePrice.Price + Desk.Type.AdditionalPrice; 
                Tickets.Add(new Ticket(schedule, Desk, TicketPrice));
            }
            if (CurrentPromotion != null)
                foreach (var CusType in CurrentPromotion.CustomerTypes)
                    if (CusType.ID == Customer.Type.ID)
                        Value = decimal.Parse((double.Parse(Value.ToString()) * CusType.PromotionDiscount).ToString());
            Save();
        }
        public TicketOrder(BookedTicket BookedTicket,Employee employee)
        {
            ID = IDGenerate(Tables.TicketOrder);
            Customer = BookedTicket.Customer;
            if (employee != null)
                Employee = employee;

            Tickets = BookedTicket.Tickets;
            Value = BookedTicket.Value;
            Save();
        }
        public TicketOrder(string _ID)
        {
            ID = _ID;
            PutTableIntoDataSet(Tables.TicketOrder);
            using (var tbl = ds.Tables[Tables.TicketOrder])
            {
                var Row = tbl.Select(TicketOrderTable.ID + "='" + ID + "'")[0];
                Customer = new Customer(Row[TicketOrderTable.CustomerID] as string);
                Employee = new Employee(Row[TicketOrderTable.EmployeeID] as string);
                OrderDate = (DateTime)Row[TicketOrderTable.OrderDate];
                Value = (decimal)Row[TicketOrderTable.Value];
            }

            PutTableIntoDataSet(Tables.Ticket);
            using (var tbl = ds.Tables[Tables.Ticket])
            {
                Tickets = new ObservableCollection<Ticket>();
                var Rows = tbl.Select(TicketTable.TicketOrder + "='" + ID + "'");
                foreach(var Row in Rows)
                {
                    Tickets.Add(new Ticket(Row[TicketTable.ID] as string));
                }
            }
            
        }
        public void Save()
        {
            //Lưu Hóa Đơn
            PutTableIntoDataSet(Tables.TicketOrder);
            using (var tbl = ds.Tables[Tables.TicketOrder])
            {
                var Row = tbl.NewRow();
                Row[TicketOrderTable.ID] = ID;
                Row[TicketOrderTable.CustomerID] = Customer.ID;
                try { Row[TicketOrderTable.EmployeeID] = Employee.ID; }
                catch
                {
                    Row[TicketOrderTable.EmployeeID] = null;
                }
                Row[TicketOrderTable.OrderDate] = DateTime.Now;
                Row[TicketOrderTable.Value] = Value;
                tbl.Rows.Add(Row);
                SaveCommand(Tables.TicketOrder);
            }

            //Lưu vé
            PutTableIntoDataSet(Tables.Ticket);
            using (var tbl = ds.Tables[Tables.Ticket])
            {
                foreach(var Ticket in Tickets)
                {
                    var Rows = tbl.Select(TicketTable.ID + "='" + Ticket.ID + "'");
                    if (Rows.Length==0)
                    {
                        var Row = tbl.NewRow();
                        Row[TicketTable.ID] = Ticket.ID;
                        Row[TicketTable.ScheduleID] = Ticket.Schedule.ID;
                        Row[TicketTable.DeskID] = Ticket.Desk.ID;
                        Row[TicketTable.TicketOrder] = ID;
                        Row[TicketTable.Price] = Ticket.Price;
                        tbl.Rows.Add(Row);                        
                    }
                    else
                    {
                        var Row = Rows[0];
                        Row[TicketTable.ID] = Ticket.ID;
                        Row[TicketTable.ScheduleID] = Ticket.Schedule.ID;
                        Row[TicketTable.DeskID] = Ticket.Desk.ID;
                        Row[TicketTable.TicketOrder] = ID;
                        Row[TicketTable.Price] = Ticket.Price;
                    }
                }
                SaveCommand(Tables.Ticket);
            } 
        }
        public static ObservableCollection<TicketOrder> Filter(DateTime from, DateTime to, string CustomerID)
        {
            PutTableIntoDataSet(Tables.TicketOrder);
            var tbl = ds.Tables[Tables.TicketOrder];
            var Rows = tbl.Select(TicketOrderTable.OrderDate + " >= '" + from + "' and " + TicketOrderTable.OrderDate + " <= '" + to + "' and " + TicketOrderTable.CustomerID + "='" + CustomerID + "'");
            var Result = new ObservableCollection<TicketOrder>();
            foreach (var Row in Rows)
            {
                Result.Add(new TicketOrder(Row[TicketOrderTable.ID] as String));
            }
            return Result;
        }
    }
}
